/////-----开票管理页面   核验/批量核验操作  ----------


import {
  getTaxInvoiceCheckSendSms, taxInvoiceCatalogById, taxSettleRevokedBatchCheck,
  taxSettleRevokedBatchSMS, taxSettleRevokedCheck, taxSettleRevokedSMS
} from '@api/invoiceAdminister'
import * as Utils from '@/utils/util'
import InvitationCode from '@views/invoice/verification/invitationCode.vue'
import InvitationCodeNew from '@views/invoice/verification/invitationCodeNew.vue'
import SingleDataVerification from '@views/invoice/verification/SingleDataVerification.vue'
import InvitationVerification from '@views/invoice/verification/InvitationVerification.vue'
import BatchVerification from '@views/invoice/verification/batchVerification.vue'

const batchVerification = {
  components:{
    InvitationCode,
    InvitationCodeNew,
    SingleDataVerification,
    InvitationVerification,
    BatchVerification
  },
  data(){
    return {
      //--------邀请验证与批量验证--------
      //批量邀请验证
      invitationVisible:  false,
      verificationList: [],
      verificationColumns: [
        {
          title: '公司法人',
          dataIndex: 'lisenceLegal',
          width: 150,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '法人实名手机号',
          dataIndex: 'legalPhone',
          width: 150,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '售票公司',
          dataIndex: 'entName',
          width: 150,
          ellipsis: true,
          align: 'center'
        },
        {
          title: '发票金额',
          dataIndex: 'amount',
          customRender: (text, record) => {
            return Math.round(text * 100) / 10000 + '元'
          },
          width: 150,
          ellipsis: true,
          align: 'center'
        },
      ],
      //邀请验证
      showCodeTitle:false,
      inContent: {},  //发票内容
      invitationShow: false,
      invitationNewShow:false,
      invitationCodeNewData:{},
      currentRowData:{},
      // 单文件核验
      singleInvitationNewShow:false,
      dataType_:'',
      singleDataVerificationData:{},
      // 批量核验
      batchVerificationNewShow:false,
      batchVerificationDataList:[],

      //---------待结算撤销---------
      //待结算撤销
      revokeConfig:{
        title:'撤销开票申请',
        width:600,
        CRloading:false,
        visible:false,
        type:'1', // 1 表示单条撤销  2 表示批量撤销
        data:{},
      },
      // 确认撤销配置
      ivConfig:{
        datas:{},
        config:{
          title:'核验邀请',
          visible:false,
        },
        data:{
          dxTitle:'短信邀请法人确认撤销', //短信标题
          dxContent:'尊敬的#企业名称#，您正在进行撤销开票申请，请点击链接确认要撤销发票的内容！<br/><a>链接1</a>', // 短信内容
          ewmTitle:'法人扫码完成确认撤销', // 二维码标题
          ewmContent:'链接', // 二维码内容
        }
      },
    }
  },
  methods:{
    //-------------------------邀请验证与批量验证  start ↓------------------
    //邀请验证
    invitationValidationNew(record){
      const {id,bizStatus} = record
      // businessStatus  record.bizStatus
      const actionList = {
        '104':()=>{
          taxInvoiceCatalogById({id}).then(res=>{
            if(res.success==true){
              if(!this.isType.isNullOrUnDef(res.result) && this.isType.isArray(res.result)){
                if(res.result.length==1){
                  //邀请验证增加弹层
                  this.inContent = record;
                  this.invitationShow = true;
                  this.showCodeTitle=true
                }
                if(res.result.length>1){
                  this.currentRowData = record
                  this.invitationCodeNewData = res.result
                  this.invitationNewShow = true
                }
              } else{
                // this.$message.warning('当前')
              }
            }else{
              this.$message.warning(res.message)
            }
          })
        },
        '118':()=>{
          // this.$message.warning('撤销核验')
          taxSettleRevokedSMS({invoiceId:id}).then(res=>{
            if(res.success==true){
              this.$message.success('核验短信已发送成功')
              this.reload();
              this.getTableData();
            }else{
              this.$message.warning(res.message || '核验短信发送失败')
            }
          })
        }
      }

      actionList[bizStatus] && actionList[bizStatus]()

      // this.invitationNewShow = true
    },
    icnEvent(type,data){
      const actionList = {
        'close':()=>{
          this.reload();
          this.getTableData();
          this.invitationNewShow = data
        },
        // 批量核验 关闭
        'SVIclose':()=>{
          const {visible,type} = data
          if(type=='close'){
            this.reload();
            this.getTableData();
          }
          this.batchVerificationNewShow = visible
        },
        // 批量核验
        'SVI':()=>{
          this.singleDataVerificationData = data
          this.dataType_ = 'SVI'
          this.singleInvitationNewShow = true
        },
        // 单独发送核验邀请
        'IV':()=>{
          this.singleDataVerificationData = data
          this.dataType_ = 'IV'
          this.singleInvitationNewShow = true
        },
        'SDVClose':()=>{
          this.singleInvitationNewShow = data
          // this.reload();
          // this.getTableData();
        },
        // 发送核验短信
        'SDVSMS':()=>{

        },
        // 拷贝二维码
        'SDVQRCode':()=>{

        },
        // 批量撤销成功，页面刷新
        'IVRefresh':()=>{
          this.reload();
          this.getTableData();
          // console.log('========================批量撤销成功，页面刷新+================================');
        }
      }
      actionList[type] && actionList[type]()
    },
    //关闭邀请验证弹层
    handleCodeFather(){
      this.$nextTick(function(){
        this.invitationShow = false;
      })

      this.reload();
      this.getTableData();
    },
    //批量验证
    batchValidation(){
      // businessStatus  record.bizStatus
      if(this.isType.isNullOrUnDef(this.selectedRows) || this.isType.isEmpty(this.selectedRows)){
        this.$message.warning('请先选择需要核验的数据')
        return false
      }
      const actionList = {
        '104':()=>{
          this.batchVerificationDataList = this.selectedRows
          this.batchVerificationNewShow = true
        },
        '118':()=>{
          // this.$message.warning('批量核验数据')
          const param = {
            "ids": this.selectedRows.map(item=>item.id),
          }
          taxSettleRevokedBatchSMS(param).then(res=>{
            if(res.success==true){
              this.$message.success('核验短信已发送成功')
              this.reload();
              this.getTableData();
            }else{
              this.$message.warning(res.message || '核验短信发送失败')
            }
          })
        },
      }

      actionList[this.businessStatus] && actionList[this.businessStatus]()

    },
    next(){
      this.InvoiceCheckSendSms(this.testArr);
      this.invitationVisible = false;
    },
    //验证接口
    InvoiceCheckSendSms(ids){
      let params = {};
      params.data = {
        ids:ids,
      }
      getTaxInvoiceCheckSendSms(params).then(res => {
        if (res.success) {
          this.$nextTick(()=>{
            this.undoConfig = {
              width: '500px',  //modal宽度
              visible: true,    //modal展示状态
              modalTitle: '邀请验证',
              title: '发送成功！',     //标题
              subTitle: '',     //副标题
              content: '请提示法人收到短信后，及时完成验证！',  //内容
              okText: '确定',    //确定文字
              okType: 'primary',    //确定类型
              cancleText: '',    //取消文字
              cancleType: '',    //取消类型
              source: 2,   //来源
            }
          });

          this.getTableData();
        }else{
          this.$message.warning(res.message);
        }
      })
    },
    //-------------------------待结算状态的撤销操作  start ↓------------------
    //撤销---待结算
    revokeEvent(record){
      this.revokeConfig.type = '1'
      this.revokeConfig.data = record
      this.revokeConfig.visible = true
      // console.log(record);
    },
    //撤销--弹层关闭
    revokeCancelEvent(){
      this.revokeConfig.visible = false
    },
    //确认撤销
    confirmRevocation(){
      this.revokeConfig.CRloading = true
      // 单条撤销
      if(this.revokeConfig.type=='1'){
        const param = {id:this.revokeConfig.data.id}
        taxSettleRevokedCheck(param).then(res=>{
          if(res.success==true){
            this.ivConfig.datas = res.result
            this.ivConfig.data.dxContent = res.result.signSMS.replace(res.result.signUrl,'') + `<br/><a href='${res.result.signUrl}'>${res.result.signUrl}</a>`
            this.ivConfig.data.ewmContent = res.result.signUrl
            this.ivConfig.config.visible = true
          }else{
            this.$message.warning(res.message)
          }
          this.revokeConfig.CRloading = false
        })
      }
      // 批量撤销
      if(this.revokeConfig.type=='2'){
        const tempIds = this.selectedRows.map(item=>item.id)
        const param = {ids:tempIds}
        taxSettleRevokedBatchCheck(param).then(res=>{
          if(res.success == true){
            this.singleDataVerificationData = res.result
            this.dataType_ = 'InvitationVerification'
            this.singleInvitationNewShow = true
          }else{
            this.$message.warning(res.message)
          }
          this.revokeConfig.CRloading = false
        })

      }
      // this.revokeConfig.visible = false
    },
    // 批量撤销
    batchRevokeEvent(){
      if(this.isType.isNullOrUnDef(this.selectedRows) || this.isType.isEmpty(this.selectedRows)){
        this.$message.warning('请选择需要撤销的数据')
        return false
      }
      this.revokeConfig.type = '2'
      this.revokeConfig.data = this.selectedRows
      this.revokeConfig.visible = true

    },
    /**
     * 邀请验证 事件处理
     */
    IVEvent(type,data){
      const actionList = {
        // 关闭
        'close':()=>{
          this.ivConfig.config.visible = data
          this.revokeConfig.visible = false
        },
        // 发送短信
        'send':()=>{
          // console.log('发送短信1====>',this.revokeConfig.data);
          taxSettleRevokedSMS({invoiceId:this.revokeConfig.data.id}).then(res=>{
            if(res.success==true){
              this.$message.success('核验短信已发送成功')
              this.reload();
              this.getTableData();
            }else{
              this.$message.warning(res.message || '核验短信发送失败')
            }
          })
        },
        // 拷贝二维码
        'CopyQRCode':()=>{
          const imgUrl = data;
          // 如果浏览器支持msSaveOrOpenBlob方法（也就是使用IE浏览器的时候），那么调用该方法去下载图片
          if (window.navigator.msSaveOrOpenBlob) {
            var bstr = atob(imgUrl.split(',')[1]);
            var n = bstr.length;
            var u8arr = new Uint8Array(n);
            while (n--) {
              u8arr[n] = bstr.charCodeAt(n);
            }
            var blob = new Blob([u8arr]);
            window.navigator.msSaveOrOpenBlob(blob, 'code' + '.' + 'png');
          } else {
            // 这里就按照chrome等新版浏览器来处理
            const a = document.createElement('a');
            a.href = imgUrl;
            a.setAttribute('download', 'code');
            a.click();
          }
        },

      }
      actionList[type] && actionList[type]()
    },

    //邀请验证---（作废）
    invitationValidation(record){
      this.inContent = record;
      //邀请验证增加弹层
      this.$nextTick(()=>{
        this.invitationShow = true;
      });
    },
  }
}

export default batchVerification
